//
//  MyTeacherViewController.swift
//  DriverSchoolAPP
//
//  Created by Logel on 2017/5/17.
//  Copyright © 2017年 Logel. All rights reserved.
//

import UIKit

class MyTeacherViewController: UIViewController,LgCallBack {
    @IBOutlet weak var lessView1: UIView!
    @IBOutlet weak var lessView2: UIView!
    @IBOutlet weak var lessView3: UIView!
    var myteacher = MyTeacher()
    @IBOutlet weak var nameLbl: UILabel!
    @IBOutlet weak var phoneLbl: UILabel!
    @IBOutlet weak var placeLbl: UILabel!
    @IBOutlet weak var weekDayLbl: UILabel!
    @IBOutlet weak var placeDailyLbl: UILabel!
    @IBOutlet weak var classTimeLbl: UILabel!
    @IBOutlet weak var time1Lbl: UILabel!
    @IBOutlet weak var time2Lbl: UILabel!
    @IBOutlet weak var time3Lbl: UILabel!
    @IBOutlet weak var price1Lbl: UILabel!
    @IBOutlet weak var price2Lbl: UILabel!
    @IBOutlet weak var price3Lbl: UILabel!
    @IBOutlet weak var cname1: UILabel!
    @IBOutlet weak var cname2: UILabel!
    @IBOutlet weak var cname3: UILabel!
    @IBOutlet weak var btn1: UIButton!
    @IBOutlet weak var btn2: UIButton!
    @IBOutlet weak var btn3: UIButton!
    var selectedBtn = 0
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        lessView1.layer.borderWidth = 1
        lessView1.layer.borderColor = UIColor.lightGray.cgColor
        
        lessView2.layer.borderWidth = 1
        lessView2.layer.borderColor = UIColor.lightGray.cgColor
        
        lessView3.layer.borderWidth = 1
        lessView3.layer.borderColor = UIColor.lightGray.cgColor
        // Do any additional setup after loading the view.
        getData()
        
       
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func getData(){
        asyn_req_post(method: M_MyTeacher, cb: self, p: ["accountId":user.accountId as AnyObject])
    }
    
    func callBack(method: String, data: AnyObject) {
        let j = JSON(data)
        print(j)
        let head = parseMobileHead(x: j["MobileHead"])
        if head.code == "1" {
            
            switch method {
            case M_MyTeacher :
                myteacher = parseMyTeacher(x: j["MobileBody"])
                freshData()
            default:
                switch selectedBtn {
                case 1:
                    btn1.isEnabled = false
                    btn1.setTitle("已申请", for: .disabled)
                    btn1.backgroundColor = UIColor.lightGray
                case 2:
                    btn2.isEnabled = false
                    btn2.setTitle("已申请", for: .disabled)
                    btn2.backgroundColor = UIColor.lightGray
                default:
                    btn3.isEnabled = false
                    btn3.setTitle("已申请", for: .disabled)
                    btn3.backgroundColor = UIColor.lightGray
                    
                }

            }

            
        }else{
            showMsg(msg: head.message)
        }
        
        
    }
    
    func freshData(){
        nameLbl.text = myteacher.teacherName
        phoneLbl.text = myteacher.teacherMobile
        placeLbl.text = myteacher.placeName
        let d = getWeekDay(dateTime: myteacher.tcList[0].classTime)
        switch d {
        case 0:
            weekDayLbl.text = "星期一"
        case 1:
            weekDayLbl.text = "星期二"
        case 2:
            weekDayLbl.text = "星期三"
        case 3:
            weekDayLbl.text = "星期四"
        case 4:
            weekDayLbl.text = "星期五"
        case 5:
            weekDayLbl.text = "星期六"
        default:
            weekDayLbl.text = "星期日"
        }
        placeDailyLbl.text = myteacher.placeName
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let date = dateFormatter.date(from: myteacher.tcList[0].classTime)
        
        let dateFormatter2 = DateFormatter()
        dateFormatter2.dateFormat = "yyyy-MM-dd"
        let ymd =  dateFormatter2.string(from: date!)
        classTimeLbl.text  = "\(ymd)"
        
        time1Lbl.text = "\(myteacher.tcList[0].startTime)-\(myteacher.tcList[0].endTime)"
        time2Lbl.text = "\(myteacher.tcList[1].startTime)-\(myteacher.tcList[1].endTime)"
        time3Lbl.text = "\(myteacher.tcList[2].startTime)-\(myteacher.tcList[2].endTime)"
        
        price1Lbl.text = "\(myteacher.tcList[0].price)/课时"
        price2Lbl.text = "\(myteacher.tcList[1].price)/课时"
        price3Lbl.text = "\(myteacher.tcList[2].price)/课时"
        
        cname1.text = "\(myteacher.tcList[0].courseName)"
        cname2.text = "\(myteacher.tcList[1].courseName)"
        cname3.text = "\(myteacher.tcList[2].courseName)"
        
        switch myteacher.tcList[0].isReServ {
        case "1":
            btn1.isEnabled = false
            btn1.setTitle("已申请", for: .disabled)
            btn1.backgroundColor = UIColor.lightGray
        default:
            if myteacher.tcList[0].status == "init"{
                btn1.isEnabled = true
                btn1.setTitle("申请预约", for: .normal)
                btn1.backgroundColor = KP_MAIN_BLUE
            }else{
                btn1.isEnabled = false
                btn1.setTitle("课程已满", for: .disabled)
                btn1.backgroundColor = UIColor.lightGray
            }
        }
        
        switch myteacher.tcList[1].isReServ {
        case "1":
            btn2.isEnabled = false
            btn2.setTitle("已申请", for: .disabled)
            btn2.backgroundColor = UIColor.lightGray
        default:
            if myteacher.tcList[1].status == "init"{
                btn2.isEnabled = true
                btn2.setTitle("申请预约", for: .normal)
                btn2.backgroundColor = KP_MAIN_BLUE
            }else{
                btn2.isEnabled = false
                btn2.setTitle("课程已满", for: .disabled)
                btn2.backgroundColor = UIColor.lightGray
            }
        }

        
        switch myteacher.tcList[2].isReServ {
        case "1":
            btn3.isEnabled = false
            btn3.setTitle("已申请", for: .disabled)
            btn3.backgroundColor = UIColor.lightGray
        default:
            if myteacher.tcList[2].status == "init"{
                btn3.isEnabled = true
                btn3.setTitle("申请预约", for: .normal)
                btn3.backgroundColor = KP_MAIN_BLUE
            }else{
                btn3.isEnabled = false
                btn3.setTitle("课程已满", for: .disabled)
                btn3.backgroundColor = UIColor.lightGray
            }

        }

        
    }
    
    @IBAction func doOrder(_ sender: UIButton) {
        selectedBtn = sender.tag
        print(myteacher.tcList[sender.tag - 1])
        
        asyn_req_post(method: M_Reserve, cb: self, p: ["accountId":user.accountId as  AnyObject,"courseId":myteacher.tcList[sender.tag - 1].id as AnyObject])
        
    }
    

}
